package controller;

import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import view.*;

import logic.*;

/**
 * this is the class RunController
 * With this class the step, stop and set buttons are set.
 * @author Vossen en Konijnen groep 2
 *
 */
public class RunController extends AbstractController implements ActionListener {
	private static final long serialVersionUID = -8776795932665582315L;
	private JButton stepOne;
	private JButton stepOneHundred;
	private JButton stopSteps;
	private JButton startSteps;
	private JButton resetSteps;
	
	/** The Constructor of RunController.
	 * Makes the Buttons with ActionListeners and places them.
	 * @param simulator
	 */
	public RunController(Simulator simulator) {
		super(simulator);
		setSize(450, 50);
		stepOne=new JButton("One step");
		stepOne.setMaximumSize(new Dimension(150,35));
		stepOne.addActionListener(this);
		stepOneHundred=new JButton("100 steps");
		stepOneHundred.setMaximumSize(new Dimension(150,35));
		stepOneHundred.addActionListener(this);
		stopSteps=new JButton("Stop");
		stopSteps.setMaximumSize(new Dimension(150,35));
		stopSteps.addActionListener(this);	
		startSteps=new JButton("Start");
		startSteps.setMaximumSize(new Dimension(150,35));
		startSteps.addActionListener(this);
		resetSteps=new JButton("Set");
		resetSteps.setMaximumSize(new Dimension(150,35));
		resetSteps.addActionListener(this);
		

        add(startSteps);
		add(stepOne);
		add(stepOneHundred);
		add(stopSteps);
		add(resetSteps);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		startSteps.setBounds(0,10,70,30);
		stepOne.setBounds(50, 10, 70, 30);
		stepOneHundred.setBounds(140, 10, 70, 30);
		stopSteps.setBounds(210,10,70,30);
		resetSteps.setBounds(280,10,70,30);
		setVisible(true);
	}

	/** 
	 * de methode voor de actionlisteners
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==startSteps) {
			try {
				simulator.simulate(1000000);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return;
		}
		
		if (e.getSource()==stepOne) {
			try {
				simulator.simulate(1);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return;
		}
		
		if (e.getSource()==stepOneHundred) {
			try {
				simulator.simulate(100);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return;
		}	
		
		if (e.getSource()==stopSteps) {
			try {
				simulator.simulate(0);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return;
		}	
			
		if (e.getSource()==resetSteps) {
			try {
				simulator.reset();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return;
		}	
	}
}
